package web.commons;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CaptchaTextField extends TextField<String> {
    private static final long serialVersionUID = 1L;

    /**
     * Log.
     */
    private static final Logger log = LoggerFactory.getLogger(CaptchaTextField.class);
    /**
     * Flag indicating whether the contents of the field should be reset each time it is rendered.
     * If <code>true</code>, the contents are emptied when the field is rendered. This is useful for
     * login forms. If <code>false</code>, the contents of the model are put into the field. This is
     * useful for entry forms where the contents of the model should be editable, or resubmitted.
     */
    private boolean reset = true;

    /**
     * @see org.apache.wicket.Component#Component(String)
     */
    public CaptchaTextField(final String id) {
        this(id, null);
    }

    /**
     * @param id
     * @param model
     * @see org.apache.wicket.Component#Component(String, IModel)
     */
    public CaptchaTextField(final String id, IModel<String> model) {
        super(id, model);
        setRequired(true);
    }

    /**
     * Flag indicating whether the contents of the field should be reset each time it is rendered.
     * If <code>true</code>, the contents are emptied when the field is rendered. This is useful for
     * login forms. If <code>false</code>, the contents of the model are put into the field. This is
     * useful for entry forms where the contents of the model should be editable, or resubmitted.
     *
     * @return Returns the reset.
     */
    public final boolean getReset() {
        return reset;
    }

    /**
     * Flag indicating whether the contents of the field should be reset each time it is rendered.
     * If <code>true</code>, the contents are emptied when the field is rendered. This is useful for
     * login forms. If <code>false</code>, the contents of the model are put into the field. This is
     * useful for entry forms where the contents of the model should be editable, or resubmitted.
     *
     * @param reset The reset to set.
     * @return <code>this</code>.
     */
    public final CaptchaTextField setReset(final boolean reset) {
        this.reset = reset;
        return this;
    }

    /**
     * Processes the component tag.
     *
     * @param tag Tag to modify
     * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag)
     */
    @Override
    protected void onComponentTag(final ComponentTag tag) {
        super.onComponentTag(tag);
        if (getReset()) {
            tag.put("value", "");
        }
    }

    /**
     * @see org.apache.wicket.markup.html.form.AbstractTextComponent#supportsPersistence()
     */
    @Override
    protected boolean supportsPersistence() {
        return false;
    }
}
